﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Data.Common
Public Class Form1

    Private Sub CategoriesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.CategoriesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.NorthwindDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'NorthwindDataSet.Employees' table. You can move, or remove it, as needed.
        Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees)
        'TODO: This line of code loads data into the 'NorthwindDataSet.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet.Customers)
        'TODO: This line of code loads data into the 'NorthwindDataSet.Categories' table. You can move, or remove it, as needed.
        Me.CategoriesTableAdapter.Fill(Me.NorthwindDataSet.Categories)

    End Sub

    Private Sub CategoryIDLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'MessageBox.Show(ComboBox1.SelectedItem.ToString)
        Dim strSQL As String = _
          "SELECT * FROM " + ComboBox1.SelectedItem.ToString

        Try
            Using adapter As New OleDbDataAdapter(strSQL, _
               My.Settings.NorthwindConnectionString)
                Dim dt As New DataTable
                adapter.Fill(dt)

                For Each dc As DataColumn In _
                 dt.Columns

                    demoList.Items.Add( _
                      String.Format("{0}", _
                      dc.ColumnName))
                Next dc

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim userTables As DataTable = Nothing

            Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()

            connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb"
            
            Dim restrictions() As String = New String(3) {}

            restrictions(3) = "Table"

            connection.Open()

            userTables = connection.GetSchema("Tables", restrictions)

            connection.Close()
            ' Add list of table names to comboBox
            Dim i As Integer

            For i = 0 To userTables.Rows.Count - 1
                ComboBox1.Items.Add(userTables.Rows(i)(2).ToString())
            Next
            Button1.Enabled = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        demoList.Items.Clear()
        Form2.Show()
    End Sub
End Class
